package models;

import static util.FindFoodUtil.empty;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import play.cache.Cache;
import play.db.jpa.Model;
import play.mvc.Scope.Session;

@Entity
@Table(name = "FindFoodUser")
public class User extends Model {

	public static User getUser(Session session) {
		if (session == null)
			return null;

		String openId = session.get("openid");
		if (empty(openId))
			return null;

		User user = Cache.get("user-" + openId, User.class);
		if (user == null) {
			user = User.find("byOpenId", openId).first();
			if (user != null)
				Cache.set("user-" + openId, user);
		}
		return user;
	}

	@Column(unique = true, nullable = false)
	private String username;

	@Column(unique = true, nullable = false)
	private String openId;

	private ZipcodeGeo home;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	public ZipcodeGeo getHome() {
		return home;
	}

	public void setHome(ZipcodeGeo home) {
		this.home = home;
	}
}
